<html xmlns="http://www.w3c.org/1999/xhtml"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	xmlns:pe="http://primefaces.org/ui/extensions">
<h:head>
	<script src="../js/jquery.maskMoney.js" type="text/javascript"></script>
	<script type="text/javascript">
		//<![CDATA[
		//debe usarse los \\:  en lugar de : en los selectores de JQuery
		$(document).ready(function() {
			wizardSetCurrentPageStyle('form_wizard:agrarianBusinessData');
			reloadFormat();
		});

			function setLabels(arg) {//Favor no cambiar los ids del formulario, de las tablas y los label
				var elementValue = arg.id.replace("a_select_input","a_select_label").replace(/:/gi, "\\:");
			    var elementBtable=arg.id.replace("a_table","b_table").replace("a_select_input","b_label").replace(/:/gi, "\\:");
			    var elementCtable=arg.id.replace("a_table","c_table").replace("a_select_input","c_label").replace(/:/gi, "\\:");
			    var elementDtable=arg.id.replace("a_table","d_table").replace("a_select_input","d_label").replace(/:/gi, "\\:");
			    var inputId= arg.id.replace("a_select_input","productDescId").replace(/:/gi, "\\:");
			    var newlabel=$("#"+elementValue).html();			    
			    if(newlabel!= null && newlabel!=""){
				    $("#"+elementBtable).html(newlabel);
				    $("#"+elementCtable).html(newlabel);
				    $("#"+elementDtable).html(newlabel);
				    $("#"+inputId).val(newlabel);
			    }		 	   
			}		
			function reloadFormat(){
				$(".currency").maskMoney({
					showSymbol : true,
					symbol : "$",
					decimal : ",",
					thousands : ".",
					precision : "0"
				});			
			}				
		//]]>
	</script>
</h:head>
<h:body>
	<h:form id="form_agrarian_business_data">
		<h2>
			<h:outputLabel
				value="#{i18n['lbl.owner.agrarian_business_perspective.title']}" />
		</h2>
		
		<p:commandButton value="#{i18n['cmpnt.new']}"
		    actionListener="#{agrarianBusinessController.createNewRegister}"
		    partialSubmit="true"
			styleClass="ui-flowbutton" update="form_agrarian_business_data" oncomplete="reloadFormat();"/>
		<p:panel id="allTables">
		<p:dataTable  id="a_table" var="register"
			value="#{aSession.agrarianResource.agrarianResourceRegisters}" emptyMessage="#{i18n['cmpnt.no.data']}">
				<p:column>
					<p:commandButton value="-" style="margin-top: 7px;"
						actionListener="#{agrarianBusinessController.deleteRegister}"
						process="@this" update=":form_agrarian_business_data:allTables">
						<f:attribute name="register" value="#{register}" />
					</p:commandButton>
				</p:column>
				<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.product']}">
				<p:selectOneMenu id="a_select" value="#{register.product}" onchange="setLabels(this);" required="true">
					<f:selectItem itemLabel="#{i18n['cmpnt.type_or_select']}"
						itemValue="" noSelectionOption="true"/>
					<f:selectItems
						value="#{productAgrarianResourceCmb.parameterDTO.options}" />						
				</p:selectOneMenu>
				<p:inputText id ="productDescId" value="#{register.productDesc}" type="hidden"/>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.variety']}">
				<p:inputText value="#{register.variety}" required="true"/>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.certified_seed']}">

				<p:selectOneMenu value="#{register.certifiedSeed}" required="true">
					<f:selectItem itemLabel="#{i18n['cmpnt.select_one']}" itemValue="" noSelectionOption="true"/>
					<f:selectItem itemLabel="#{i18n['cmpnt.yes']}" itemValue="1" />
					<f:selectItem itemLabel="#{i18n['cmpnt.not']}" itemValue="2" />
				</p:selectOneMenu>

			</p:column>
			<p:column styleClass="text-align-center"
				headerText="Area cosechada(ha)">
				<p:inputText styleClass="input-number ui-width-xxxs" value="#{register.harvestedArea}" required="true">
						<pe:keyFilter mask="pnum"/> 	
				</p:inputText>
			</p:column>
			
			<p:column styleClass="text-align-center"
				headerText="Producción Total (kilos/año) Semestre A">
				<p:inputText styleClass="input-number ui-width-xxxs" value="#{register.semesterA}" required="true">
				<pe:keyFilter mask="pnum"/> 	
				</p:inputText>
				
			</p:column>
			<p:column styleClass="text-align-center"
				headerText="Producción Total (kilos/año) Semestre B">
				<p:inputText styleClass="input-number ui-width-xxxs" value="#{register.semesterB}" required="true">
					<pe:keyFilter mask="pnum"/> 	
				</p:inputText>				
			</p:column>
		</p:dataTable>

		<br />
		<br />
		<br />
		<p:dataTable id="b_table" var="register"
			value="#{aSession.agrarianResource.agrarianResourceRegisters}" emptyMessage="#{i18n['cmpnt.no.data']}">
			<p:column headerText="">
				<p:outputLabel id="b_label" styleClass="label-product"  value="#{register.productDesc}" />
			</p:column>			
			<p:column styleClass="text-align-center"
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.establishment_costs']}">
				<p:inputText  value="#{register.establishmentCosts}" required="true" styleClass="currency ui-width-s"/>
			</p:column>
			<p:column styleClass="text-align-center"
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sustenance_cost']}">
				<p:inputText value="#{register.sustainingCosts}" required="true" styleClass="currency ui-width-s"/>

			</p:column>
			<p:column styleClass="text-align-center"
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.production_for_the_market']}">
				<p:inputText styleClass="input-number ui-width-s" value="#{register.productionForTheMarket}" required="true">
						<pe:keyFilter mask="pnum"/> 	
				</p:inputText>				
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.site_for_sale']}">
				<p:selectOneMenu value="#{register.siteForSale}" required="true">
					<f:selectItem itemLabel="#{i18n['cmpnt.select_one']}" itemValue="" noSelectionOption="true"/>
					<f:selectItems
						value="#{siteForSaleAgrarianResourceCmb.parameterDTO.options}" />
					<f:selectItem itemLabel="#{i18n['cmpnt.other']}"
						itemValue="3"  />						
				</p:selectOneMenu>
			</p:column>
		</p:dataTable>
		<br />
		<br />
		<br />
		<p:dataTable id="c_table" var="register"
			value="#{aSession.agrarianResource.agrarianResourceRegisters}" emptyMessage="#{i18n['cmpnt.no.data']}">
			<p:column headerText="">
				<p:outputLabel id="c_label" styleClass="label-product"  value="#{register.productDesc}" />
			</p:column>
<!-- 			<p:column -->
<!-- 				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sells_to']}"> -->
<!-- 				<p:inputText value="#{register.sellsToNameAndSurname}" /> -->
<!-- 			</p:column> -->
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sale_type']}">
				<p:selectOneMenu value="#{register.saleType}" required="true">
					<f:selectItem itemLabel="#{i18n['cmpnt.select_one']}" itemValue="" noSelectionOption="true"/>
					<f:selectItems
						value="#{saleTypeAgrarianResourceCmb.parameterDTO.options}" />
					<f:selectItem itemLabel="#{i18n['cmpnt.other']}"
						itemValue="3"  />							
				</p:selectOneMenu>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sells_to_name_and_surname']}">
				<p:inputText value="#{register.sellsToNameAndSurname}" required="true"/>
			</p:column>
			<p:column styleClass="text-align-center"
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.average_sales_price']}">
				<p:inputText value="#{register.salesPriceAverage}" required="true" styleClass="currency ui-width-s"/>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.method_of_payment']}">
				<p:selectOneMenu value="#{register.methodOfPayment}" required="true">
					<f:selectItem itemLabel="#{i18n['cmpnt.select_one']}" itemValue="" noSelectionOption="true"/>
					<f:selectItems
						value="#{methodOfPaymentForestResourceCmb.parameterDTO.options}" />
				</p:selectOneMenu>
			</p:column>
		</p:dataTable>
		<br />
		<br />
		<br />
		<p:dataTable id="d_table" var="register"
			value="#{aSession.agrarianResource.agrarianResourceRegisters}" emptyMessage="#{i18n['cmpnt.no.data']}">
			<p:column headerText="">
				<p:outputLabel id="d_label" styleClass="label-product"  value="#{register.productDesc}" />
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sub_product_crop']}">
				<p:inputText value="#{register.subProduct}" required="true"/>
			</p:column>
			<p:column 
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.sub_product_use']}">
				<p:inputText value="#{register.subProductUse}" required="true"/>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.membership_technical_assistance_programs']}">
				<p:selectOneMenu
					value="#{register.membershipTechnicalAssistancePrograms}" id="radioAssistance" onchange="block(this);">
					<f:selectItem itemLabel="#{i18n['cmpnt.yes']}" itemValue="1" />
					<f:selectItem itemLabel="#{i18n['cmpnt.not']}" itemValue="2" />
				</p:selectOneMenu>
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.assistance_program']}">
				<p:inputText value="#{register.assistanceProgram}" id="program" />
			</p:column>
			<p:column
				headerText="#{i18n['lbl.owner.agrarian_business_perspective.assistance_entity']}">
				<p:inputText value="#{register.assistanceEntity}" id="entity"/>
			</p:column>
		</p:dataTable>
		</p:panel>
		<br />
		<br />		
		
		
		<div class="buttons-container clearfix text-align-right">
			<p:commandButton value="#{i18n['lbl.flow_button.cancel']}"
				styleClass="ui-flowbutton"
				oncomplete="currentPage('list_owner_data.xhtml')"
				actionListener="#{stepByStepController.cancelRuatRegister}" />
			<p:commandButton value="#{i18n['lbl.flow_button.back']}"
				oncomplete="setCurrentPageStyle(xhr, status, args,this)"
				styleClass="ui-flowbutton" update="form_agrarian_business_data"
				actionListener="#{stepByStepController.viewFarmData}" />
			<p:commandButton value="#{i18n['lbl.flow_button.save_and_next']}"
				oncomplete="setCurrentPageStyle(xhr, status, args,this)"
				styleClass="ui-flowbutton" validateClient="true"
				update="form_agrarian_business_data"
				actionListener="#{stepByStepController.viewAnimalBusinessData}" 
				id="savenext" />
		</div>
		<p:blockUI block="savenext" trigger="savenext"/>
	</h:form>
</h:body>
	<script type="text/javascript">
		//<![CDATA[
		//debe usarse los \\:  en lugar de : en los selectores de JQuery

		function block(arg) {
	 	   		var selectorId= (arg.id).replace(/:/gi, "\\:");
	 	   		var programid = selectorId.replace("radioAssistance_input","program");
	 	   		var entityid = selectorId.replace("radioAssistance_input","entity");
	 			var valor = $("#"+selectorId).val();	
	 			
	 	   		if(valor==1){
	 	   			$("#"+entityid).removeAttr('disabled').removeClass('ui-state-disabled');
	 				$("#"+programid).removeAttr('disabled').removeClass('ui-state-disabled');
	 	   		}else{
		 	   		$("#"+entityid).attr('disabled', 'true').addClass('ui-state-disabled');
		 	   		$("#"+programid).attr('disabled', 'true').addClass('ui-state-disabled');
	 	   		}

			}		
				
		//]]>
	</script>
</html>